// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
//import star from "./star"
//import player from "./player";
@ccclass
export default class game extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    @property({
        displayName: "star spine node", 
        type: cc.Prefab 
    })
    starPrefab: cc.Prefab = null
    // 地面节点，用于确定星星生成的高度
    @property({
        displayName: "ground node",
        type: cc.Node
    })

    ground: cc.Node = null

    // Player 节点，用于获取主角弹跳的高度，和控制主角行动开关
    @property({
        displayName: "player node",
        type: cc.Node
    })
    player: cc.Node = null

    @property({
        displayName: "Score (player)", 
        tooltip: "The score of player"
    })
    score = 0;

    @property({
        displayName: "Score label", 
        type: cc.Label,
    })
    scoreDisplay: cc.Label = null


    // 星星产生后消失时间的随机范围
    @property
    maxStarDuration = 0;
    @property
    minStarDuration = 0;

    groundY = 0;
    timer = 0
    starDuration = 0

     // 得分音效资源
     @property({
        type: cc.AudioClip
    })
    scoreAudio : cc.AudioClip

    @property({
        type: cc.Node
    })
    btnNode: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // 获取地平面的 y 轴坐标
        console.log("ground.y:",this.ground.y)
        console.log("this.ground.height /2:",this.ground.height/2)
        this.groundY = this.ground.y + this.ground.height/2;
        // 生成一个新的星星
        //this.spawnNewStar();
        this.score = 0
        // 初始化计时器
        this.timer = 0;
        this.starDuration = 0;
    }

    start () {
        //this.label.string = "hello label"         
        //console.log(this.label)
        console.log(this.text)

       // let newLabel = this.node.addComponent(cc.Label)
       // newLabel.string = "hello world"
        //this.label.string = "Hello Label"
        
        console.log("---start---")
    }

    onStartGame () {
        // 初始化计分
        this.score = 0;
        // set game state to running
        //this.isPlaying = true;
        // set button and gameover text out of screen
        this.btnNode.x = 3000;
        //this.gameOverNode.active = false;
        // reset player position and move speed
        //this.player.startMoveAt(cc.v2(0, this.groundY));
        this.player.getComponent('player').startMoveAt(cc.v2(0, this.groundY));
        // spawn star
        this.spawnNewStar();
    }

    spawnNewStar() {
        cc.log('---spawnNewStar---')
        // 使用给定的模板在场景中生成一个新节点
        var newStar = cc.instantiate(this.starPrefab);
        // // 将新增的节点添加到 Canvas 节点下面
        this.node.addChild(newStar);
        // // 为星星设置一个随机位置
        newStar.setPosition(this.getNewStarPosition());
        newStar.getComponent('star').game = this;
        
        // 重置计时器，根据消失时间范围随机取一个值
        this.starDuration = this.minStarDuration + Math.random() * (this.maxStarDuration - this.minStarDuration);
        this.timer = 0;
    }

    getNewStarPosition() {
        cc.log('---getNewStarPosition---')
        var randX = 0;
        // 根据地平面位置和主角跳跃高度，随机得到一个星星的 y 坐标
        var height = this.player.getComponent('player').jumpHeight;
        var randY = this.groundY + Math.random() * height + 50;
        // 根据屏幕宽度，随机得到一个星星 x 坐标
        var maxX = this.node.width/2;
        randX = (Math.random() - 0.5) * 2 * maxX;
        // 返回星星坐标
       // return cc.v2(10, 10);
       return cc.v2(randX, randY);
    }

    gainScore() {
        this.score += 1;
        // 更新 scoreDisplay Label 的文字
        this.scoreDisplay.string = 'Score: ' + this.score;

         // 播放得分音效
         cc.audioEngine.playEffect(this.scoreAudio, false);
    }
    
    gameOver () {
        // 停止 Player 节点的跳跃动作
        this.player.stopAllActions(); 
        // 重新加载场景 game
        cc.director.loadScene('game');
    }

    update (dt) {

         // 每帧更新计时器，超过限度还没有生成新的星星
        // 就会调用游戏失败逻辑
        if (this.timer > this.starDuration && this.starDuration != 0) {
            this.gameOver();
            return;
        }
        this.timer += dt
    }
}
